//
//  UIStackView_Demo2VC.swift
//  APSwift
//
//  Created by jianbo.pei on 2022/8/3.
//

import UIKit
import Then
import SnapKit

class UIStackView_Demo2VC: UIViewController {

    private var agreeProtocolStackView: APLoginAgreeProtocolStackView!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        agreeProtocolStackView = APLoginAgreeProtocolStackView().then({ make in
            view.addSubview(make)
            make.snp.makeConstraints { make in
//                make.left.right.equalToSuperview()
                make.top.equalTo(120)
                make.height.equalTo(80)
                make.centerX.equalToSuperview()
            }
        })
    }
    

}


class APLoginAgreeProtocolStackView: UIStackView {
    private var checkboxButton: UIButton!
    
    public override init(frame: CGRect) {
        super.init(frame: frame)
        axis = .horizontal
        alignment = .center
        
        checkboxButton = UIButton(type: .custom).then({ make in
            addArrangedSubview(make)
            make.snp.makeConstraints { make in
                make.size.equalTo(28)
            }
            make.setImage(UIImage(named: "gb_login_protocol_disagree"), for: .normal)
            make.setImage(UIImage(named: "gb_login_protocol_agree"), for: .selected)
            make.addTarget(self, action: #selector(didTappedCheckBox), for: .touchUpInside)
        })
        
        _ = UILabel().then({ make in
            addArrangedSubview(make)
            make.textColor = .black
            make.font = .systemFont(ofSize: 12)
            make.text = "同意"
        })
        
        _ = UIButton(type: .custom).then({ make in
            addArrangedSubview(make)
            make.setTitle("《用户协议》", for: .normal)
            make.titleLabel?.font = .systemFont(ofSize: 12)
            make.setTitleColor(.blue, for: .normal)
            make.addTarget(self, action: #selector(didTappedUserProtocol), for: .touchUpInside)
        })
        
        _ = UILabel().then({ make in
            addArrangedSubview(make)
            make.textColor = .black
            make.font = .systemFont(ofSize: 12)
            make.text = "和"
        })
        
        _ = UIButton(type: .custom).then({ make in
            addArrangedSubview(make)
            make.setTitle("《隐私政策》", for: .normal)
            make.titleLabel?.font = .systemFont(ofSize: 12)
            make.setTitleColor(.blue, for: .normal)
            make.addTarget(self, action: #selector(didTappedUserProtocol), for: .touchUpInside)
        })
    }
    
    @objc private func didTappedCheckBox() {
        debugPrint("点击了 - checkBox")
        checkboxButton.isSelected = !checkboxButton.isSelected
    }
    
    @objc private func didTappedUserProtocol() {
        debugPrint("点击了 - 用户协议")
    }
    
    @objc private func didTappedUserPrivacyPolicy() {
        debugPrint("点击了 - 隐私政策")
    }
    
    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
